/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// const YAML = require('./snakeyaml').YAML
import YAML from './snakeyaml'

const BASE_COUNT=100

 
/* YAML Parse */
function parse_Test_01() {  
    let testData = '3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196'
    let result; 
    let startTime = Date.now()
    for (let i=0;i<1000;i++) {
        result = YAML.parse(testData) 
    } 
    let endTime = Date.now() 
    let time = endTime - startTime
    print("snakeyaml_parse(string): " + time +" ms")  
    areEqual('3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 8214808651 3282306647 0938446095 5058223172 5359408128 4811174502 8410270193 8521105559 6446229489 5493038196',String(result)) 
   
    // print(result)
}
/* YAML Parse */
function parse_Test_02() {  
    let testData = '[ snakeyaml是一个用于解析YAML类型数据的功能库。 支持： Parse & Stringify, YAML Documents等,0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F, G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z, true, false, maybe, 3.1415926, null ]\n'
    let result; 
    let startTime = Date.now()
    for (let i=0;i<BASE_COUNT;i++) {
        result = YAML.parse(testData) 
    } 
    let endTime = Date.now() 
    let time = endTime - startTime
    print("snakeyaml_parse(array): " + time +" ms")  
    areEqual('snakeyaml是一个用于解析YAML类型数据的功能库。 支持： Parse & Stringify,YAML Documents等,0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,true,false,maybe,3.1415926,',String(result)) 
   
    // print(result)
}
 
/* YAML Stringify */
function stringify_Test_01() { 
    let testData = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
    let result; 
    let startTime = Date.now()
    for (let i=0;i<1000;i++) {
        result = YAML.stringify(testData)
    } 
    let endTime = Date.now() 
    let time = endTime - startTime
    print("snakeyaml_stringify(string): " + time +" ms")  
    areEqual('3.141592653589793\n',result)

    // print(result)  
 
} 
/* YAML Stringify */
function stringify_Test_02() { 
    let src = ['Stringify', 'YAML',0,1,2,3,4,5,6,7,8,9,'A', 'B', 'C', 'D', 'E', 'F', 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',true, false, 'maybe', null]
    let result; 
    let startTime = Date.now()
    for (let i=0;i<BASE_COUNT;i++) {
        result = YAML.stringify(src)
    } 
    let endTime = Date.now() 
    let time = endTime - startTime 
    print("snakeyaml_stringify(array): " + time +" ms")  
    areEqual('- Stringify\n- YAML\n- 0\n- 1\n- 2\n- 3\n- 4\n- 5\n- 6\n- 7\n- 8\n- 9\n- A\n- B\n- C\n- D\n- E\n- F\n- G\n- H\n- I\n- J\n- K\n- L\n- M\n- N\n- O\n- P\n- Q\n- R\n- S\n- T\n- U\n- V\n- W\n- X\n- Y\n- Z\n- true\n- false\n- maybe\n- null\n',result)

    // print(`${result}`)  
 
} 
/* YAML Stringify */
function stringify_Test_03() { 
    let result; 
    let startTime = Date.now()
    for (let i=0;i<BASE_COUNT;i++) {
        result = YAML.stringify({ array: [true, false, 'maybe', null], number: 3.1415926, plain: 'string', block: 'two\nlines\n' })
    } 
    let endTime = Date.now() 
    let time = endTime - startTime
    print("snakeyaml_stringify(js): " + time +" ms")  
    areEqual('array:\n  - true\n  - false\n  - maybe\n  - null\nnumber: 3.1415926\nplain: string\nblock: |\n  two\n  lines\n',result)

    // print(result)  
 
} 

/* YAML Document */
function Document_Test() {  
    let doc;   
    let startTime = Date.now()
    for (let i=0;i<10000;i++) {
        doc = new YAML.Document() 
    } 
    let endTime = Date.now() 
    let time = endTime - startTime
    print("snakeyaml_Document: " + time +" ms")  
    doc.version = "true"
    doc.commentBefore = ' A commented document'
    doc.contents = ['some', 'values', { array: [true, false, 'maybe', null], number: 3.1415926, plain: 'string', block: 'two\nlines\n' }]
    areEqual('# A commented document\n\n%YAML 1.2\n---\n- some\n- values\n- array:\n    - true\n    - false\n    - maybe\n    - null\n  number: 3.1415926\n  plain: string\n  block: |\n    two\n    lines\n',String(doc)) 
    // print(String(doc))
}

/* YAML Document Parse  */
function parseDocument_Test_01() {   
    let doc
    let src = '[{ a: A }, { b: B },- a: # 123,some, values, {balloons: 99},snakeyaml是一个用于解析YAML类型数据的功能库。 支持： Parse & Stringify, YAML Documents等,0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F, G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z, true, false, maybe, 3.1415926, null]'   
    let startTime = Date.now()
    for (let i=0;i<BASE_COUNT;i++) {
        doc = YAML.parseDocument(src)
    }  
    let endTime = Date.now()
    let time = endTime - startTime 
    print("snakeyaml_parseDocument(src): " + time +" ms") 
    areEqual('[{"a":"A"},{"b":"B"},{"- a":null}]', JSON.stringify(doc.contents)) 
    // print(JSON.stringify(doc.contents))   
} 
/* YAML Document Parse  */
function parseDocument_Test_02() {   
    let doc
    let src = '[{ a: A }, { b: B }, { c: C }, { d: D }, { e: E }, { f: F }, { g: G }, { h: H }, { i: I }, { j: J }, { k: K }, { l: L }, { m: M }, { n: N }, { o: O }, { p: P }, { q: Q }, { r: R }, { s: S }, { t: T }, { u: U }, { v: V }, { w: W }, { x: X }, { y: Y }, { z: Z }]'
    let startTime = Date.now()
    for (let i=0;i<BASE_COUNT;i++) {
        doc = YAML.parseDocument(src,null)
    }  
    let endTime = Date.now()
    let time = endTime - startTime 
    print("snakeyaml_parseDocument(src,null): " + time +" ms") 
    areEqual('[{"a":"A"},{"b":"B"},{"c":"C"},{"d":"D"},{"e":"E"},{"f":"F"},{"g":"G"},{"h":"H"},{"i":"I"},{"j":"J"},{"k":"K"},{"l":"L"},{"m":"M"},{"n":"N"},{"o":"O"},{"p":"P"},{"q":"Q"},{"r":"R"},{"s":"S"},{"t":"T"},{"u":"U"},{"v":"V"},{"w":"W"},{"x":"X"},{"y":"Y"},{"z":"Z"}]', JSON.stringify(doc.contents)) 
    // print(JSON.stringify(doc.contents))   
} 
 
/* Expected results and actual results verification  */
function areEqual(expected, actual) {
    // If expected and actual are both NaN, consider them equal.
    if (expected === actual || expected !== expected && actual !== actual) {
        // print('Success')
    } else {
        let error = 
        '  Failed areEqual test' +
        '  Expected: ' + expected +
        '  Actual:   ' + actual
        ;
        throw(error) 
    }
}

// function print(s){
//     console.log(s)
// }

/*  Benchmark  */
function snakeyamlRun() { 
    parse_Test_01()  
    parse_Test_02()  

    stringify_Test_01()  
    stringify_Test_02()
    stringify_Test_03()  

    Document_Test()    

    parseDocument_Test_01()    
    parseDocument_Test_02()    
} 
let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    snakeyamlRun()
}

ArkTools.waitAllJitCompileFinish();

snakeyamlRun()